import request from '@/utils/request';
import {API_ORDER} from './config';
import {Order} from './types';

// 订单查询参数
export interface OrderQueryParams {
    pageNum?: number;
    pageSize?: number;
    orderNumber?: string;
    supplierName?: string;
    status?: string;
    createTimeStart?: string;
    createTimeEnd?: string;
    purchaseDate?: string;
}

// // 订单明细项
// export interface OrderItem {
//     id?: string;
//     orderId?: string;
//     productName: string;
//     productCode?: string;
//     specification?: string;
//     unit: string;
//     quantity: number;
//     price: number;
//     amount: number;
//     remark?: string;
// }

// // 订单数据
// export interface OrderData {
//     id?: string;
//     orderNumber: string;
//     customerName: string;
//     customerCode?: string;
//     customerContact?: string;
//     customerPhone?: string;
//     customerAddress?: string;
//     orderDate: string;
//     deliveryDate?: string;
//     status?: string;
//     totalAmount: number;
//     remark?: string;
//     createBy?: string;
//     createTime?: string;
//     updateBy?: string;
//     updateTime?: string;
//     items: OrderItem[];
// }

/**
 * 获取订单列表
 * @param params 查询参数
 */
export function getOrderList(params: OrderQueryParams) {
    return request({
        url: API_ORDER.PAGE,
        method: 'get',
        params
    });
}

/**
 * 获取订单详情
 * @param id 订单ID
 */
export function getOrderDetail(id: string) {
    return request({
        url: API_ORDER.DETAIL(id),
        method: 'get'
    });
}

/**
 * 创建订单
 * @param data 订单数据
 */
export function createOrder(data: Order) {
    return request({
        url: API_ORDER.CREATE,
        method: 'post',
        data
    });
}

/**
 * 更新订单
 * @param id 订单ID
 * @param data 订单数据
 */
export function updateOrder(id: string, data: Order) {
    return request({
        url: API_ORDER.UPDATE(id),
        method: 'put',
        data
    });
}

/**
 * 删除订单
 * @param id 订单ID
 */
export function deleteOrder(id: string) {
    return request({
        url: API_ORDER.DELETE(id),
        method: 'delete'
    });
}

/**
 * 批量删除订单
 * @param ids 订单ID数组
 */
export function batchDeleteOrder(ids: string[]) {
    return request({
        url: API_ORDER.BATCH_DELETE,
        method: 'delete',
        data: ids
    });
}

/**
 * 导出订单
 * @param params 查询参数
 */
export function exportOrder(params: OrderQueryParams) {
    return request({
        url: API_ORDER.EXPORT,
        method: 'get',
        params,
        responseType: 'blob',
        timeout: 60000, // 延长超时时间为60秒
        headers: {
            'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'X-Export-Request': 'true' // 明确标记这是导出请求
        }
    });
}

// /**
//  * 获取订单状态统计
//  */
// export function getOrderStatusStats() {
//     return request({
//         url: API_ORDER.STATISTICS,
//         method: 'get'
//     });
// }

/**
 * 获取订单关联单据
 * @param orderId 订单ID
 */
export function getOrderRelatedDocuments(orderId: string) {
    return request({
        url: `${API_ORDER.BASE}/${orderId}/related-documents`,
        method: 'get'
    });
} 